home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Hacks / Hacks ’92 / Text Capture FKEY / Prefs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-19  |  4.1 KB  |  156 lines  |  [TEXT/KAHL]

  1. #include <Folders.h>
  2. #include "defs.h"
  3.  
  4. #include "Prefs.h"
  5.  
  6. #define        ALLOCATE_RESOURCE_DATA    1
  7. #include "embedded resources.h"
  8.  
  9.  
  10. /*
  11.     This routine is called in two modes:
  12.     * Usually, we just need to get the settings from the prefs file,
  13.       and then close it.
  14.     * If, however, we are going to be using the configuration dialog,
  15.       we want to leave the file open.
  16. */
  17.  
  18. void    Get_prefs( file_disposition disp, Handle my_h )
  19. {
  20.     long    pref_dirID;
  21.     short    pref_vRefNum;
  22.     short    save_resfile;
  23.     Boolean    newfile = false;
  24.     Boolean    **config_h;
  25.     Handle    new_res_h;
  26.     long    total_size, contig_size;
  27.     FInfo    info;
  28.     Str255    pref_name;
  29.     short        my_id;
  30.     ResType        my_type;
  31.     
  32.     save_resfile = CurResFile();
  33.     pref_resfile = -1; // if this stays -1, it indicates failure
  34.     
  35.     // Contruct the prefs file name from the resource name
  36.     GetResInfo( my_h, &my_id, &my_type, pref_name );
  37.     BlockMove( " Settings", &pref_name[ pref_name[0] + 1 ], 9 );
  38.     pref_name[0] += 9;
  39.  
  40.     FindFolder( kOnSystemDisk,'pref', kCreateFolder,
  41.             &pref_vRefNum, &pref_dirID );
  42.  
  43.     /*
  44.         At this point, unless something awful has happened, we have the
  45.         dirID and vRefNum of the appropriate folder.  Next we try to
  46.         find the file by name.
  47.     */
  48.     pref_resfile = HOpenResFile( pref_vRefNum, pref_dirID,
  49.         pref_name,
  50.         (disp == p_read_only)? fsRdPerm : fsRdWrPerm );
  51.  
  52.     /*
  53.         If we didn't find the file, we need to create it.
  54.     */
  55.     if (pref_resfile == -1)
  56.     {
  57.         HCreateResFile( pref_vRefNum, pref_dirID, pref_name );
  58.         /*
  59.             Since we look for the file by name, we lock the name.
  60.             May as well set the type and creator here too.
  61.         */
  62.         (void) HGetFInfo( pref_vRefNum, pref_dirID, pref_name, &info );
  63.         info.fdFlags |= 0x1000; /* nameLocked */
  64.         info.fdType = 'pref';
  65.         info.fdCreator = '????';
  66.         (void) HSetFInfo( pref_vRefNum, pref_dirID, pref_name, &info );
  67.         /*
  68.             The file is ready, open it so we can add resources.
  69.         */
  70.         newfile = true;
  71.         pref_resfile = HOpenResFile( pref_vRefNum, pref_dirID,
  72.             pref_name, fsRdWrPerm );
  73.         if (pref_resfile == -1)
  74.         {
  75.             SysBeep(1);
  76.             return;    /* give up */
  77.         }
  78.     }
  79.  
  80.     /*
  81.         If we just created this file, we need to create a default
  82.         configuration resource in it, otherwise we read the config.
  83.     */
  84.     if (newfile)
  85.     {
  86.         PurgeSpace( &total_size, &contig_size );
  87.         if (total_size < 2000)
  88.         {
  89.             SysBeep(1);
  90.             return;
  91.         }
  92.         config_h = (Boolean **) NewHandle( 3 );
  93.         (*config_h)[0] = c_selected;
  94.         (*config_h)[1] = false;    // separate lines with hard returns
  95.         (*config_h)[2] = false;    // fake activate event
  96.         AddResource( (Handle) config_h, CONFIG_TYPE, 128, "\p" );
  97.  
  98.         new_res_h = NewHandle( sizeof(DITL_data) );
  99.         BlockMove( DITL_data, *new_res_h, sizeof(DITL_data) );
  100.         AddResource( new_res_h, 'DITL', DITL_ID, "\p" );
  101.  
  102.         new_res_h = NewHandle( sizeof(DLOG_data) );
  103.         BlockMove( DLOG_data, *new_res_h, sizeof(DLOG_data) );
  104.         AddResource( new_res_h, 'DLOG', DLOG_ID, "\p" );
  105.  
  106.         new_res_h = NewHandle( sizeof(PICT_data) );
  107.         BlockMove( &PICT_data, *new_res_h, sizeof(PICT_data) );
  108.         AddResource( new_res_h, 'PICT', OUTLINE_PICT_ID, "\p" );
  109.  
  110.         new_res_h = NewHandle( sizeof(gray_PICT_data) );
  111.         BlockMove( &gray_PICT_data, *new_res_h, sizeof(gray_PICT_data) );
  112.         AddResource( new_res_h, 'PICT', GRAY_PICT_ID, "\p" );
  113.  
  114.         new_res_h = NewHandle( sizeof(text_PICT_data) );
  115.         BlockMove( &text_PICT_data, *new_res_h, sizeof(text_PICT_data) );
  116.         AddResource( new_res_h, 'PICT', TEXT_PICT_ID, "\p" );
  117.  
  118.         new_res_h = NewHandle( sizeof(copy_PICT_data) );
  119.         BlockMove( ©_PICT_data, *new_res_h, sizeof(copy_PICT_data) );
  120.         AddResource( new_res_h, 'PICT', COPY_PICT_ID, "\p" );
  121.  
  122.         UpdateResFile( pref_resfile );
  123.         FlushVol( NIL, pref_vRefNum );
  124.     }
  125.     else
  126.     {
  127.         config_h = (Boolean **) Get1Resource( CONFIG_TYPE, 128 );
  128.     }
  129.     copy_mode = (*config_h)[0];
  130.     spaces = (*config_h)[1];
  131.     fake_activate = (*config_h)[2];
  132.     
  133.     UseResFile( save_resfile );
  134.     if (disp == p_read_only)
  135.     {
  136.         CloseResFile( pref_resfile );
  137.     }
  138. }
  139.  
  140.  
  141.  
  142. void    Set_prefs( void )
  143. {
  144.     short    save_resfile;
  145.     Boolean    **config_h;
  146.     
  147.     save_resfile = CurResFile();
  148.     UseResFile( pref_resfile );
  149.     config_h = (Boolean **) Get1Resource( CONFIG_TYPE, 128 );
  150.     (*config_h)[0] = copy_mode;
  151.     (*config_h)[1] = spaces;
  152.     (*config_h)[2] = fake_activate;
  153.     ChangedResource( (Handle) config_h );
  154.     UpdateResFile( pref_resfile );
  155.     UseResFile( save_resfile );
  156. }